<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Huati
 *
 * @package App
 * @mixin \Eloquent
 */
class Huati extends Model
{
    public $timestamps = false;
    protected $table = 'tbl_huati';


    //是否推荐
    const HOT_YES = 1;
    const HOT_NO = 0;
    static $HotDisplay = [
        self::HOT_YES => '推荐',
        self::HOT_NO => '不推荐',
    ];

    /**
     * 话题列表
     * @return \Illuminate\Support\Collection
     */
    static function Huatis()
    {
        return Huati::select('id', 'title')->get();
    }

    /**
     * 修改话题
     * @param $request
     * @param $huati
     * @return array
     */
    static function ModifyHuati($request, $huati)
    {
        $huati->p_id = $request->p_id;
        $huati->title = $request->title;
        $huati->title2 = $request->title2;
        $huati->introduce = $request->introduce;
        $huati->weight = $request->weight;
        //是否推荐
        if ($request->has('hot')) {
            $huati->hot = self::HOT_YES;
        } else {
            $huati->hot = self::HOT_NO;
        }

        //图片
        if ($request->has('pic')) {
            $pic = Storage::url(Postings::OssUpdate($request->pic));
            $huati->pic = $pic;

        }
        //背景图片
        if ($request->has('bg_pic')) {
            $bg_pic = Storage::url(Postings::OssUpdate($request->bg_pic));
            $huati->bg_pic = $bg_pic;

        }

        if (!$huati->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        HuatiOwner::where('huati_id', $huati->id)->update([
            'circle_name' => $request->title
        ]);

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 增加话题
     * @param $request
     * @return array
     */
    static function HuatiPublish($request)
    {
        $huati = new Huati();
        $huati->p_id = $request->p_id;
        $huati->title = $request->title;
        $huati->title2 = $request->title2;
        $huati->introduce = $request->introduce;
        $huati->weight = $request->weight;
        $huati->ctime = date('Y-m-d H:i:s');
        //是否推荐
        if ($request->has('hot')) {
            $huati->hot = self::HOT_YES;
        } else {
            $huati->hot = self::HOT_NO;
        }

        //图片
        if ($request->has('pic')) {
            $pic = Storage::url(Postings::OssUpdate($request->pic));
            $huati->pic = $pic;
        } else {
            return ['status' => 2, 'message' => '失败'];
        }
        //背景图片
        if ($request->has('bg_pic')) {
            $bg_pic = Storage::url(Postings::OssUpdate($request->bg_pic));
            $huati->bg_pic = $bg_pic;
        } else {
            return ['status' => 2, 'message' => '失败'];
        }

        if (!$huati->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }
}
